<?php
/**
 * 主要的功能逻辑
 */
class Horse
{
    /**
     * 表名
     *
     * @var string
     */
    public $table = "";
    public $db    = '';
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->table = "index";
        try {
            $this->db = GetDB();
        } catch (Exception $e) {
            echo __CLASS__ . "数据库初始化失败";
        }

    }
    /**
     * 插入一条数据
     *
     * @param string $p1  第一匹马的技能+状态
     * @param string $p2  第二匹马的技能+状态
     * @param string $win 赢得马的技能+状态
     */
    public function add($p1, $p2, $win)
    {
        $data['P1']  = $p1;
        $data['P2']  = $p2;
        $data['WIN'] = $win;
        return $this->db->Insert($this->table, $data);
    }

    public function jisuan($p1, $p2)
    {
        $sql = "select `P1`,`P2`,`WIN` from `" . $this->table . "` WHERE (P1='" . $p1 . "' AND P2='" . $p2 . "') OR (P2='" . $p1 . "' AND P1='" . $p2 . "')";
        $ret = $this->db->Query($sql);
        if ($ret) {
            $pc1   = 0;
            $pc2   = 0;
            $count = count($ret);
            foreach ($ret as $key => $value) {
            	if($value['WIN']==$p1){
            		$pc1++;
            	}else{
            		$pc2++;
            	}
            }
           	$p1win=round($pc1/$count*100,2)."%"; 
           	$p2win=round($pc2/$count*100,2)."%";
           	return array($p1win,$p2win); 
        }else{
        	return false;
        }
    }

}
